package com.yellowpage.wf.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.wf.dao.WorkflowNodeInstanceDao;
import com.yellowpage.wf.data.WorkflowNodeInstance;

public class WorkflowNodeInstanceServiceImpl extends BaseServiceImpl implements WorkflowNodeInstanceService {
	  private WorkflowNodeInstanceDao workflowNodeInstanceDao;

	  public void setWorkflowNodeInstanceDao(WorkflowNodeInstanceDao workflowNodeInstanceDao) {
		  this.workflowNodeInstanceDao = workflowNodeInstanceDao;
	  }


	  /* White generate Method */
	  public WorkflowNodeInstance getWorkflowNodeInstance(Long id){
	      return workflowNodeInstanceDao.get(id);
	  }

    public List<WorkflowNodeInstance> getAllWorkflowNodeInstance(){
        return workflowNodeInstanceDao.getAll();
    }

    public List<Map> queryWorkflowNodeInstanceByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT a.WORKFLOW_HISTORY_ID , a.WORKFLOW_NODE_ID , a.WORKFLOW_INSTANCE_ID , a.WORKER , a.CREATE_USER , a.CREATE_TIME    FROM WF_WORKFLOW_NODE_INSTANCE a WHERE 1=1 ")
				    			.addCondition(" and WORKFLOW_HISTORY_ID = :workflowHistoryId ")
							    			.addCondition(" and WORKFLOW_NODE_ID = :workflowNodeId ")
							    			.addCondition(" and WORKFLOW_INSTANCE_ID = :workflowInstanceId ")
							    			.addCondition(" and WORKER = :worker ")
							    			.addCondition(" and CREATE_USER = :createUser ")
							    		    .addCondition(" and CREATE_TIME = to_date(:createTime ,'yyyy-MM-dd') ")
		    				    ;

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}


    public void saveWorkflowNodeInstance(WorkflowNodeInstance workflowNodeInstance){
        workflowNodeInstanceDao.save(workflowNodeInstance);
    }

	  public void updateWorkflowNodeInstance(WorkflowNodeInstance workflowNodeInstance){
	      workflowNodeInstanceDao.update(workflowNodeInstance);
	  }

	  public void delete(List deleteList){
	      workflowNodeInstanceDao.remove(deleteList);
	  }


	public List<Map> getAllWorkflowNodeInstanceByInstanceId(Long workflowInstanceId) {
//		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
//				"SELECT a.WORKFLOW_HISTORY_ID , a.WORKFLOW_NODE_ID , a.WORKFLOW_INSTANCE_ID , " +
//				" a.WORKER , a.CREATE_USER , a.CREATE_TIME ,b.name WORKFLOW_NODE_NAME " +
//				" from wf_workflow_node_instance a , wf_workflow_node b  " +
//				" where  1=1  " +
//				" and a.workflow_node_id=b.workflow_node_id  " +
//				" and a.workflow_instance_id = "+workflowInstanceId.toString()+
//				" order by  a.workflow_history_id desc "
//				);
		//List<Map> resultList = sqlAssistant.query(baseDao.getConnection(), new HashMap());
		String sql = "SELECT a.WORKFLOW_HISTORY_ID , a.WORKFLOW_NODE_ID , a.WORKFLOW_INSTANCE_ID , " +
		" a.WORKER , a.CREATE_USER , a.CREATE_TIME ,b.name WORKFLOW_NODE_NAME " +
		" from wf_workflow_node_instance a , wf_workflow_node b  " +
		" where  1=1  " +
		" and a.workflow_node_id=b.workflow_node_id  " +
		" and a.workflow_instance_id = "+workflowInstanceId.toString()+
		" order by  a.workflow_history_id desc ";
		List<Map> resultList = this.baseDao.query(sql, new HashMap());

		return resultList;
	}


	public String getOwner(String sql , String deptCode) {
		String owners="";
		if(sql.indexOf("@") >= 0) 
			sql = sql.replaceAll("@", deptCode);
		System.out.println("sql:"+sql);
		//QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(sql);
		
		
		//List<Map> resultList = sqlAssistant.query(baseDao.getConnection(), new HashMap());
		
		List<Map> resultList = this.baseDao.query(sql, new HashMap());
		
		if(resultList!=null)
		System.out.println("size:--------->"+resultList.size());

		for(Map m : resultList){
			System.out.println(m.get("EMPLOYEE_ID"));
			owners+= m.get("EMPLOYEE_ID") + ",";
		}
		
		return owners.substring(0, owners.lastIndexOf(","));
	}
	public List<Map> getNodeInstanceId(Long instanceId,Long nodeId){
		
		String sql = " SELECT WNI.NODE_INSTANCE_ID FROM " +
				" WF_WORKFLOW_NODE_INSTANCE WNI " +
				" WHERE WNI.WORKFLOW_NODE_ID = "+nodeId.toString()+
				" AND WNI.WORKFLOW_INSTANCE_ID = "+instanceId.toString() ;
		List<Map> resultList = this.baseDao.query(sql, new HashMap());

		return resultList;
	}
	
	
	public static void main(String[] args) {
		String str = "aaaaa@aaa,";
		System.out.println(str.replaceAll("@", "111"));
		System.out.println(str.lastIndexOf(","));
		System.out.println(str.substring(0,str.lastIndexOf(",")));
	}


}